/*==========================================================================*
|                  				 ERROR DESP.        	 					|
| UNIT: smserr.c							                                |
| AUTH: JOHN.LEE															|
|---------------------------------------------------------------------------|
| INFO: Copyright (c) 2006, AGREE CO. LTD. 									|
|		All rights reserved.        										|
*==========================================================================*/
#include <errno.h>
#include <pthread.h>

#include "smsinc.h"
#include "smserr.h"

typedef struct
{
	BYTE	USE;		//Usable;
	INT		ENO;		//errno;
	UINT	ECD;		//Err code
	UINT	LNE;		//line num
	BYTE	FLE[64];	//file name;
	BYTE	FUN[64];	//func
}SMSERRDESP,*PSMSERRDESP;

typedef struct
{
    UINT            Size;
    BYTE            Flag[4];
    pthread_mutex_t mtx;
    FILE            *hFile;   
    UINT            MaxSize;
}SMSLOGCTX,*PSMSLOGCTX;

SMSERRDESP	GLB_VAR_ERR;
SMSLOGCTX   GLB_VAR_LOG;




void SMS_clrerror()
{
	memset(&GLB_VAR_ERR,0,sizeof(GLB_VAR_ERR));	
}

void SMS_seterror(PBYTE _file_,PBYTE _func_,UINT _line_,UINT _usreno_)
{
	if(ISNULL(_file_)||ISNULL(_func_))
	{
		return;	
	}
	
	GLB_VAR_ERR.USE			= 0xFF;
	GLB_VAR_ERR.ENO			= errno;
	GLB_VAR_ERR.ECD			= _usreno_;
	GLB_VAR_ERR.LNE			= _line_;
	snprintf((char *)GLB_VAR_ERR.FLE,sizeof(GLB_VAR_ERR.FLE),"%s",_file_);
	snprintf((char *)GLB_VAR_ERR.FUN,sizeof(GLB_VAR_ERR.FUN),"%s",_func_);
}

void SMS_geterror(PBYTE _file_,PBYTE _func_,PUINT _line_,PUINT _usreno_,PINT _errno_)
{
	if(_file_!=NULL)
	{
		strcpy((char *)_file_,(char *)GLB_VAR_ERR.FLE);	
	}	
	
	if(_func_!=NULL)
	{
		strcpy((char *)_func_,(char *)GLB_VAR_ERR.FUN);	
	}
	
	if(_line_!=NULL)
	{
		_line_[0] = GLB_VAR_ERR.LNE;	
	}
	
	if(_usreno_!=NULL)
	{
		_usreno_[0] = GLB_VAR_ERR.ECD;	
	}
	
	if(_errno_!=NULL)
	{
		_errno_[0] = GLB_VAR_ERR.ENO;	
	}
}

UINT SMS_crtlog(PBYTE file,UINT maxsize)
{
    if(ISNULL(file))
    {
        return (__LINE__);   
    }
   
    memset(&GLB_VAR_LOG,0,sizeof(GLB_VAR_LOG));
    GLB_VAR_LOG.Size = sizeof(GLB_VAR_LOG);
    pthread_mutex_init(&(GLB_VAR_LOG.mtx),NULL); 
    
    GLB_VAR_LOG.Flag[0] = 0xFF; 
    
    GLB_VAR_LOG.hFile = fopen((char *)file,"a+");
    if(ISNULL(GLB_VAR_LOG.hFile))
    {
        return (__LINE__);   
    }
    
    GLB_VAR_LOG.Flag[1] = 0xFF; 
    
    ftruncate(fileno(GLB_VAR_LOG.hFile),0);
    GLB_VAR_LOG.MaxSize = maxsize;
    
    return (0);
}

void SMS_clslog()
{
    if(GLB_VAR_LOG.hFile!=NULL)
    {
        fflush(GLB_VAR_LOG.hFile);
        fclose(GLB_VAR_LOG.hFile);   
    }  
    pthread_mutex_destroy(&(GLB_VAR_LOG.mtx));
    memset(&GLB_VAR_LOG,0,sizeof(GLB_VAR_LOG)); 
}

static PBYTE _GetNow(PBYTE now,PBYTE fmt)
{
	BYTE			szTmp[8];
	struct	tm 		tme;
	struct timeb 	timebuffer;
	time_t 			t;
	PBYTE			ptr;
	

	t = time(0);
	ftime(&timebuffer);
	
	memcpy( &tme, localtime(&t), sizeof(struct tm) );
	strcpy((char *)now,(char *)fmt);
	
	if ( (ptr = (unsigned char *)strstr((char *)now, "YYYY" ))!=NULL )
	{
		sprintf( (char *)szTmp, "%04d", tme.tm_year+1900 );
		memcpy( ptr, szTmp, 4 );
	}
	else if ( (ptr = (unsigned char *)strstr( (char *)now, "YY" ))!=NULL )
	{
		sprintf( (char *)szTmp, "%02d", (tme.tm_year+1900)%100 );
		memcpy( ptr, szTmp, 2 );
	}

	if ( (ptr = (unsigned char *)strstr( (char *)now, "MM" ))!=NULL )
	{
		sprintf( (char *)szTmp, "%02d", tme.tm_mon+1 );
		memcpy( ptr, szTmp, 2 );
	}

	if ( (ptr = (unsigned char *)strstr( (char *)now, "DD" ))!=NULL )
	{
		sprintf( (char *)szTmp, "%02d", tme.tm_mday );
		memcpy( ptr, szTmp, 2 );
	}

	if ( (ptr = (unsigned char *)strstr( (char *)now, "hh" ))!=NULL )
	{
		sprintf( (char *)szTmp, "%02d", tme.tm_hour );
		memcpy( ptr, szTmp, 2 );
	}

	if ( (ptr = (unsigned char *)strstr( (char *)now, "mm" ))!=NULL )
	{
		sprintf( (char *)szTmp, "%02d", tme.tm_min );
		memcpy( ptr, szTmp, 2 );
	}

	if ( (ptr = (unsigned char *)strstr( (char *)now, "ss" ))!=NULL )
	{
		sprintf( (char *)szTmp, "%02d", tme.tm_sec );
		memcpy( ptr, szTmp, 2 );
	}
	
	if ( (ptr = (unsigned char *)strstr( (char *)now, "xxx" ))!=NULL )
	{
		sprintf( (char *)szTmp, "%03hu",timebuffer.millitm );
		memcpy( ptr, szTmp, 3 );
	}
	
	return (now);
}

void SMS_writeerror(PBYTE RiseFile,UINT RiseLine,PBYTE Memo)
{
    BYTE	szTmp[32];

    pthread_mutex_lock(&(GLB_VAR_LOG.mtx));

    if(GLB_VAR_LOG.hFile!=NULL)
    {
		if(ftell(GLB_VAR_LOG.hFile)>=GLB_VAR_LOG.MaxSize)
		{
			ftruncate(fileno(GLB_VAR_LOG.hFile),0);
			rewind(GLB_VAR_LOG.hFile);
		}
		fprintf(GLB_VAR_LOG.hFile,"\n----------------------------- ERROR REPORT -----------------------------\nRISE = %s(%u)\nPSID = %06u\nFILE = %s\nFUNC = %s\nLINE = %u\nECDE = 0x%08X\nSYEN = %d\nSYER = %s\nTIME = %s\nMEMO = %s\n------------------------------------------------------------------------\n", \
		    RiseFile,RiseLine,getpid(),GLB_VAR_ERR.FLE,GLB_VAR_ERR.FUN,GLB_VAR_ERR.LNE,GLB_VAR_ERR.ECD,GLB_VAR_ERR.ENO,strerror(GLB_VAR_ERR.ENO),_GetNow(szTmp,(unsigned char *)"YYYY/MM/DD hh:mm:ss"),Memo);
		fflush(GLB_VAR_LOG.hFile);
    }
    
    pthread_mutex_unlock(&(GLB_VAR_LOG.mtx));
}

void SMS_writelog(PBYTE RiseFile,UINT RiseLine,PBYTE Memo)
{
    BYTE	szTmp[32];

    pthread_mutex_lock(&(GLB_VAR_LOG.mtx));
    if(GLB_VAR_LOG.hFile!=NULL)
    {
		if(ftell(GLB_VAR_LOG.hFile)>=GLB_VAR_LOG.MaxSize)
		{
			ftruncate(fileno(GLB_VAR_LOG.hFile),0);
			rewind(GLB_VAR_LOG.hFile);
		}
    fprintf(GLB_VAR_LOG.hFile,"[PID:%08d][%s] -- [FILE:%s][LINE:%06u][%s]\n",(INT)getpid(),_GetNow(szTmp,(unsigned char *)"YYYY/MM/DD hh:mm:ss"),RiseFile,RiseLine,Memo);
		fflush(GLB_VAR_LOG.hFile);
    } 
    pthread_mutex_unlock(&(GLB_VAR_LOG.mtx)); 
}

//add by linp 20110825
void SMS_writehexlog(PBYTE RiseFile,UINT RiseLine,PBYTE buff, UINT len)
{
  BYTE	szTmp[32];

  int   ii,iLen,iCount=1;

  //OpenMyLogFile();
  pthread_mutex_lock(&(GLB_VAR_LOG.mtx));
  if(GLB_VAR_LOG.hFile!=NULL){
  	fprintf(GLB_VAR_LOG.hFile,"[%s][%d][%s][%d]Hex Data:\n","now time",(INT)getpid(),RiseFile,RiseLine);
    //fprintf(GLB_VAR_LOG.hFile,"[%s][%d][%s][%d]Hex Data:\n",My_GetNow(szTmp,(unsigned char *)"MMDD hhmmss xxx"),(INT)getpid(),RiseFile,RiseLine);

    iLen=0;
    for ( ii=0; ii<len; ii++ ){
      if (iLen==0) fprintf( GLB_VAR_LOG.hFile, "%04d:", iCount);
      fprintf( GLB_VAR_LOG.hFile, "%02x ", (unsigned char)buff[ii]);
      iLen+=3; iCount++;
      if ( iLen >= 75 ){
        fprintf( GLB_VAR_LOG.hFile, "\n" );
        iLen=0;
      }
    }
    if (iLen>0) fprintf( GLB_VAR_LOG.hFile, "\n");
    fflush( GLB_VAR_LOG.hFile );
  } 
  pthread_mutex_unlock(&(GLB_VAR_LOG.mtx)); 
}
